/**
 * Auth controller js code
 */
define(function (require,exports,module){
	require("ajaxproxy");
	require("layer");
	var common = require('common');

	exports.init = function(configs){

		//提交验证
		$('[data-act=do_auth]').on("click", function() {
			var data = $("#authFrom").serialize();
			var action = "do_auth";
			$.post(action, data, function (result) {
				var ajax = $.parseJSON(result);
				if( ajax.code=="000" ){
					layer.msg("提交成功");
					location.reload();
				}else{
					if( !!ajax.message ){
						layer.alert(ajax.message,{icon:2});
					}else{
						layer.alert("提交失败",{icon:2});
					}
				}
			});
		});

		//获取验证码
		$('[data-act=get_code]').on("click",function(){
			var mobile = $("[name=mobile]").val();
			var _this = $(this);
			if( mobile.length == 11 ){
				if(/^1[3|4|5|8][0-9]\d{8}$/.test(mobile)){
					$.post("getCode",{mobile:mobile},function(result){
						var data = $.parseJSON(result);
						if( data.code == "000" ){
							_this.addClass("erCodeInpGray").prop("disabled",true).html('重新获取（<i>120s</i>）');
							layer.msg("验证码已发出，请注意查收!<br/>如未收到，请重新获取",{shift: -1},
								function(l,i) {
								setTimeout(function () {
									layer.close(i)
								}, 3000)});
							//倒计时
							exports.loadSecond(_this.find('i'), 120, function () {
								_this.removeClass("erCodeInpGray").prop('disabled', false).html('重新获取短信');
								return false;
							});
						}else{
							if( !!data.message ){
								layer.alert(data.message,{icon:2});
							}else{
								layer.alert("发送失败，请重试！",{icon:2});
							}
						}
					});
				}else{
					layer.alert("手机号码不正确！",{icon:2});
				}
			}else if( 0<mobile.length && mobile.length <11){
				layer.alert("手机号码不正确！",{icon:2});
			}

		});
        window.setInterval(function(){
            var val = $("[name=mobile]").val()
            if(val.length == 11){
                $('[data-act=get_code]').removeClass("erCodeInpGray").prop('disabled',false);
            }else{
                $('[data-act=get_code]').addClass("erCodeInpGray").prop('disabled',true);
            }
        },300);
		//校验手机号码
		$("[name=mobile]").on("blur",function () {
			var mobile = $(this).val();
			if( mobile.length == 11 ){
				if(/^1[3|4|5|8][0-9]\d{8}$/.test(mobile)){
					$('[data-act=get_code]').removeClass("erCodeInpGray").prop('disabled',false);
				}else{
					layer.alert("手机号码不正确！",{icon:2});
				}
			}else if( 0<mobile.length && mobile.length <11){
				layer.alert("手机号码不正确！",{icon:2});
			}
		})
	};

	exports.loadSecond = function(jqObj,secs,callback){
		var i=secs;
		var t= setInterval(function(){
			if(i == 0) {
				clearInterval(t);
				if(typeof(callback) == "function") callback();
			}else{
				//jqObj.text('请等待 ' + i + ' s');
				jqObj.text(i + "s");
			}
			i--;
		},1000);
	};

});
